package com.glory.cqaospda.viewmodel

import com.glory.cqaospda.base.Constant
import com.glory.cqaospda.model.TaskRepository
import com.glory.mvvmnet.base.BaseViewModel
import com.glory.mvvmnet.entity.CallObserveBean
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.JsonObject

class StartjobModel: BaseViewModel() {

    private val repository by lazy { TaskRepository() }

    fun defaultLotInfo(lotID: String,isLot:String,EquipmentName:String) {
        val map = JsonObject()
        map.addProperty("Lotid", lotID)
        map.addProperty("EquipmentName", EquipmentName)
        map.addProperty("BatchLotType", if (isLot == "etLotId")"MainLot" else "ResidueLot")

        launchOnlyResult({
            val defaultLotInfo =
                repository.defaultLotInfo(map,"EMS_GetLotInfo")

            when(isLot){
                "etLotId"->{
                    defUI.callObserve.value = CallObserveBean(1,defaultLotInfo)
                }
                "etLotsId"->{
                    if ("Y".equals(defaultLotInfo.mIsResidue)){
                        defUI.callObserve.value = CallObserveBean(1,defaultLotInfo)
                    }else{
                        defUI.toastEvent.value = ToastMessageBean("当前批次为尾料",true)
                    }
                }
            }
        })
    }

    fun defaultEqpInfo(eqpID: String) {
        val map = JsonObject()
        map.addProperty("EqpId", eqpID)
        launchWithError({
            val defaultEqpInfo =
                repository.defaultEqpInfo(map,"EMS_GetEqpInfo")
            Constant.eqpid = eqpID
            defUI.callObserve.value = CallObserveBean(3,defaultEqpInfo)
        },{

        },true)
    }

    fun defaultBatchTrackIn(map: JsonObject,url:String) {
         launchOnlyResult({
             val trackInInfoBean = repository.defaultBatchTrackIn(map,url)
             defUI.callObserve.value = CallObserveBean(2,trackInInfoBean)
        })
    }

    fun defaultEqpMaterials(eqpID: String) {
        val map = JsonObject()
        map.addProperty("EquipmentName", eqpID)
        launchOnlyResult({
            val defaultEqpMaterials =
                repository.defaultEqpMaterials(map,"EMS_EqpMaterials")
            defUI.callObserve.value = CallObserveBean(4,defaultEqpMaterials)
        })
    }

    fun defaultBOM(map: JsonObject) {
        launchOnlyResult({
            val bom = repository.defaultBOM(map,"EMS_BOM")
            defUI.callObserve.value = CallObserveBean(5,bom)
        })
    }

}